package org.joyflow.core.controller;

import lombok.RequiredArgsConstructor;
import org.joyflow.core.common.R;
import org.joyflow.core.entity.Db;
import org.joyflow.core.entity.DbConType;
import org.joyflow.core.entity.DbDir;
import org.joyflow.core.entity.DbType;
import org.joyflow.core.instance.DbConTypeRegistry;
import org.joyflow.core.instance.DbTypeRegistry;
import org.joyflow.core.service.DbServiceInterface;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/db")
@RequiredArgsConstructor
public class DbController {
    private final DbServiceInterface dbService;
    @RequestMapping("/types")
    public R types() {
        List<DbType> types = DbTypeRegistry.instance().getTypes();
        return R.success(types);
    }

    @RequestMapping("/conTypes")
    public R conTypes() {
        List<DbConType> types = DbConTypeRegistry.instance().getTypes();
        return R.success(types);
    }

    @RequestMapping("/dbDirs")
    public R dbDirs() {
        List<DbDir> dirs = dbService.getDirs();
        return R.success(dirs);
    }

    @RequestMapping("/dbs")
    public R dbs() {
        List<Db> dbs = dbService.getDbs();
        return R.success(dbs);
    }

    @PostMapping("/test")
    public R test(@RequestBody Db db) {
        return R.success(dbService.test(db));
    }

    @RequestMapping("/execStatement")
    public R execStatement( Long dbId, String statement) {
        return R.success(dbService.execStatement(dbId,statement));
    }
}
